package com.hspedu.tankgame04;

import java.util.List;
import java.util.Vector;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/14
 * @time 19:22
 */
public class EnemyTank extends Tank implements Runnable{

    List<Bullet> bullets = new Vector();

    boolean isAlive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
        this.setType(0);
    }

    @Override
    public void run() {


        while (true){

            //判断集合是否还有子弹
            //没有的话就创建子弹并添加
            if (isAlive && bullets.size() == 0){
                Bullet bullet = null;

                switch (getDirection()){
                    case 0 :
                        bullet = new Bullet(getX()+20,getY(),0);
                        break;
                    case 1:
                        bullet = new Bullet(getX()+60,getY()+20,1);
                        break;
                    case 2:
                        bullet = new Bullet(getX()+20,getY()+60,2);
                        break;
                    case 3:
                        bullet = new Bullet(getX(),getY()+20,3);
                        break;
                }
                bullets.add(bullet);
                new Thread(bullet).start();
            }

            //0 1 2 3 上 右 下 左
            switch (getDirection()){
                case 0:
                    for (int i = 0; i < 30; i++) {
                            moveUp();
                        //线程创建后每休眠五十毫秒就修改一次坐标
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                            moveRight();
                        //线程创建后每休眠五十毫秒就修改一次坐标
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                            moveDown();
                        //线程创建后每休眠五十毫秒就修改一次坐标
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                            moveLeft();
                        //线程创建后每休眠五十毫秒就修改一次坐标
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }

            int direction = (int) (Math.random() * 4);
            setDirection(direction);

            //线程退出的条件
            if (!isAlive){
                break;
            }
        }
    }
}
